/*
 * Created by JFormDesigner on Mon Dec 16 16:43:22 CST 2024
 */

package findbook;
import java.awt.event.*;
import javax.swing.*;
import net.miginfocom.swing.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import net.miginfocom.swing.*;

/**
 * @author jjchen
 */
public class findbook_author extends JFrame {
    private JTextArea resultArea; // 新增的文本区域用于显示结果

    public findbook_author() {
        initComponents();
    }

    private void button1MouseClicked(MouseEvent e) {
        this.resultArea = new JTextArea();
        this.add(this.resultArea);

        String writerName = textField1.getText();
        String url = "jdbc:mysql://localhost:3306/javaproject";
        String username = "root";
        String password = "root";

        String sql = "SELECT * FROM book_information WHERE writer = ?";

        try (Connection conn = DriverManager.getConnection(url, username, password);
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            // 设置查询参数
            pstmt.setString(1, writerName);

            // 执行查询
            ResultSet rs = pstmt.executeQuery();

            // 清空结果区域并准备添加结果
            resultArea.setText("");

            // 添加标题行
            resultArea.append("Search Results:\n");
            resultArea.append("ID\tTitle\tWriter\tPublication Year\n");
            resultArea.append("-------------------------------------\n");

            // 遍历结果集并添加到结果区域
            while (rs.next()) {
                // 假设book_information表有id, title, writer, publication_year等字段
                String id = rs.getString("book_id");
                String title = rs.getString("book_title");
                String writer = rs.getString("writer");
                String publicationYear = rs.getString("storage_time");

                // 将结果格式化为字符串并添加到结果区域，使用制表符\t进行对齐
                resultArea.append(id + "\t" + title + "\t" + writer + "\t" + publicationYear + "\n");
            }

        } catch (SQLException ex) {
            // 处理SQL异常
            resultArea.setText("Error: " + ex.getMessage());
            ex.printStackTrace();
        }
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();

        //======== this ========
        var contentPane = getContentPane();
        contentPane.setLayout(new MigLayout(
            "hidemode 3",
            // columns
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]" +
            "[fill]",
            // rows
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]" +
            "[]"));

        //---- label1 ----
        label1.setText("\u8bf7\u8f93\u5165\u8981\u67e5\u627e\u7684\u4f5c\u8005");
        contentPane.add(label1, "cell 0 4");
        contentPane.add(textField1, "cell 4 4 9 1");

        //---- button1 ----
        button1.setText("\u641c\u7d22");
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button1MouseClicked(e);
            }
        });
        contentPane.add(button1, "cell 4 6");
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }
    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
